package com.wang.java.basic.framework.queue.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * 数组阻塞队列
 * 数据结构：数组
 * 线程安全：是，基于ReentrantLock实现
 * 队列长度：有界，固定大小
 * 阻塞性：阻塞
 */
public class ArrayBlockingQueueTest {
    public static void main(String[] args) throws InterruptedException {
        // 队列满时，尝试插入
//        testInsert();
        // 队列为空时，尝试删除
//        testDelete();
        // 测试遍历
        testGet();
    }

    /**
     * 队列满时，尝试插入
     * add：Exception in thread "main" java.lang.IllegalStateException: Queue full
     * offer：返回false
     * put：阻塞
     */
    private static void testInsert() throws InterruptedException {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(1);
        arrayBlockingQueue.add("a");
        arrayBlockingQueue.add("b");
//        arrayBlockingQueue.offer("b");
        arrayBlockingQueue.put("b");
        System.out.println("inseret done!");
    }

    /**
     * 队列为空时，尝试删除
     * remove：Exception in thread "main" java.util.NoSuchElementException
     * poll：返回false
     * take：阻塞
     */
    private static void testDelete() throws InterruptedException {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(1);
//        arrayBlockingQueue.remove();
//        arrayBlockingQueue.poll();
//        arrayBlockingQueue.take();
        System.out.println("delete done!");
    }

    /**
     * 队列为空时，尝试获取元素
     * element：Exception in thread "main" java.util.NoSuchElementException
     * peek：返回null
     */
    private static void testGet() {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(1);
//        arrayBlockingQueue.element();
        arrayBlockingQueue.peek();
    }
}
